package library.and.xps.com.step.view.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.autolayout.AutoRelativeLayout;

import java.net.URL;

import io.realm.Realm;
import library.and.xps.com.step.App;
import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.RequestParams;
import library.and.xps.com.step.m.model.Team;
import library.and.xps.com.step.m.model.User;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseActivity;

/**
 * Created by XPS on 2017/6/7.
 */

public class ResetTeamPassActivity extends BaseActivity {
    AutoRelativeLayout actionbar_al;
    SwitchCompat switch_compat;
    EditText et_pass;
    View v_hint;
    TextView tv_commit_pass;

    @Override
    protected int getLayout() {
        return R.layout.reset_teampass;
    }

    @Override
    protected void setData() {
        if(!TeamHelper.getIns().hasTeam())return;
        Team team =  TeamHelper.getIns().getCurT();
        if(team.isPassWordEnable()){
            switch_compat.setChecked(true);
        }else {
            switch_compat.setChecked(false);
        }
    }

    @Override
    protected void init() {
        actionbar_al = r2v(R.id.actionbar_al);
        et_pass = r2v(R.id.et_pass);
        v_hint = r2v(R.id.v_hint);
        tv_commit_pass = r2v(R.id.tv_commit_pass);
        switch_compat = r2v(R.id.switch_compat);
        switch_compat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switch_compat.isChecked()) {
                    toogleJointTeamPass(0);
                } else {
                    toogleJointTeamPass(-1);
                }
            }
        });
        tv_commit_pass.setOnClickListener(this);
        r2v(R.id.actionbar_iv_back).setOnClickListener(this);
        ((TextView) r2v(R.id.actionbar_tv_title)).setText("团队密码");

    }

    void toogleJointTeamPass(final int state){
        String json = RequestParams.getNewInstance().setUserid(getUser().getUserid()).setTeamid(TeamHelper.getIns().getTid()).setIsno(state+"").getJson().toString();

        OkHttpClientUtils.postJsonStringAsync(this, URLConstants.TEAM.UpdateTeamInformation, json, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if(HttpResponsHelper.SuccessResponce(response)){
                    if(state==0){
                        enablePass();
                    }else if(state==-1) {
                        disablePass();
                    }
                }
            }

            @Override
            public void onStart() {

            }
        },"");
    }

    void commitNewPass(String newpass){

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

        String json = RequestParams.getNewInstance().setUserid(getUser().getUserid()).setTeamid(TeamHelper.getIns().getTid()).setPassword(newpass).getJson().toString();

        OkHttpClientUtils.postJsonStringAsync(this, URLConstants.TEAM.UpdateTeamInformation, json, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if(HttpResponsHelper.SuccessResponce(response)){
                    Snackbar.make(switch_compat, "密码已修改", Snackbar.LENGTH_LONG)
                            .setAction("确定", null).show();
                }
            }

            @Override
            public void onStart() {

            }
        },"");
    }

    void enablePass() {
        et_pass.setEnabled(true);
        Snackbar.make(switch_compat, "已开启密码", Snackbar.LENGTH_LONG)
                .setAction("确定", null).show();
        et_pass.setTextColor(Color.parseColor("#000000"));
        v_hint.setBackgroundColor(Color.parseColor("#06D5A6"));
        tv_commit_pass.setEnabled(true);
        tv_commit_pass.setTextColor(Color.parseColor("#000000"));

        getmRealm().executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                User user =   realm.where(User.class).findFirst();
                if (user != null && null!=user.getTeams()&&user.getTeams().size()>0) {
                    user.getTeams().get(0).setPassWordEnable(true);
                }
            }
        });

    }

    void disablePass() {
        et_pass.setEnabled(false);
        Snackbar.make(switch_compat, "已关闭密码", Snackbar.LENGTH_LONG)
                .setAction("确定", null).show();
        v_hint.setBackgroundColor(Color.parseColor("#f4f4f4"));
        tv_commit_pass.setEnabled(false);
        tv_commit_pass.setTextColor(Color.parseColor("#f4f4f4"));
        et_pass.setTextColor(Color.parseColor("#f4f4f4"));

        getmRealm().executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                User user =   realm.where(User.class).findFirst();
                if (user != null && null!=user.getTeams()&&user.getTeams().size()>0) {
                    user.getTeams().get(0).setPassWordEnable(false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.tv_commit_pass:
                commitNewPass(et_pass.getText().toString());
                break;
        }
    }

}
